package cn.sheep.dolphin.tag

import org.apache.commons.lang.StringUtils
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.sql.Row

/** 跟广告的相关的标签
  * author: old sheep
  * QQ: 64341393 
  * Created 2018/12/2
  */
object Tags4Ads extends Tags {
	/**
	  * 定义个标签接口
	  *
	  * @param args
	  * @return
	  */
	override def makeTags(args: Any*): Map[String, Int] = {
		var tagMap = Map[String, Int]()
		// 解析参数
		val row = args(0).asInstanceOf[Row]
		val stopwordBT = args(1).asInstanceOf[Broadcast[Map[String, AnyRef]]]

		// 广告位
		val adSpaceType = row.getAs[Int]("adspacetype")
		val adSpaceTypeName = row.getAs[String]("adspacetypename")
		if (adSpaceType > 0 && adSpaceType < 10) tagMap += (("LC0" + adSpaceType, 1)) else if (adSpaceType >= 10) tagMap += (("LC" + adSpaceType, 1))
		if (StringUtils.isNotEmpty(adSpaceTypeName)) tagMap += "LN" + adSpaceTypeName -> 1


		// 渠道
		val channelId = row.getAs[Int]("adplatformproviderid")
		if (channelId > 0) tagMap += "CN" + channelId -> 1

		// 关键字
		row.getAs[String]("keywords")
			.split("\\|")
			.filter(k => k.size >= 3 && !stopwordBT.value.contains(k))
			.foreach(k => tagMap += "K" + k -> 1)


		tagMap
	}
}
